<!--
 * @title: 会议详情组件，可以进行会议的暂存、发布、作废等操作
 * @使用场景: 例如某个审批节点需要创建会议，并且该会议上传会议纪要后才可以通过该节点，则可以通过该组件进行发布会议，判断会议是否上传会议纪要
 * @params: sourceType
 * @params: sourceId，来源id
 * @params: sourceApp，来源系统
 * @params: attendCodes，参会人员codes
 * @params: attendNames，参会人员names
 * @params: disableEditSubmit，是否不可编辑
 * @params: meetId，会议id，新增时不要传参，即使传参也只能传数字0或空字符串
 * @params: buttonName 按钮名称，默认显示“发布会议”
 * @params: isDisplayDefaultElement 是否显示按钮，默认true
 * @params: render 会议组件类型，默认el-dialog
 * @params: width 组件宽度，默认1000px，render为div时生效，且父组件必须使用sync修饰符修饰
 * @params: height 组件高度，默认700px，render为div时生效，且父组件必须使用sync修饰符修饰
 * @params: saveMeet函数，父组件需要绑定该函数，当暂存或发布会议成功后会通过该函数通知父组件，参数为会议信息，需要根据不同业务需求记录在表中
 * @params: cancelMeet函数，父组件需要绑定该函数，当作废会议成功后会通过该函数通知父组件，参数为会议id，需要将表中数据清除
 * @params: isUploadedRecord函数，父组件需要绑定该函数，初始加载该组件后会通过该函数通知父组件，参数为会议是否上传会议纪要，根据该结果判断是否能够审批通过
 * @author: chengengwei
 * @date: 2021-10-22
!-->
<template>
  <div class="tl-button-ref-container">
    <slot>
      <el-button v-if="isDisplayDefaultElement" type="primary" icon="el-icon-s-custom" @click="openMeetEdit">{{ buttonName }}</el-button>
    </slot>
    <div v-show="display" :style="render === 'div' ? 'width:' + width + ';height:' + height : 'height:100vh;width:100vw;position:fixed;z-index:1020;top:0;left:0'">
      <tl-iframe ref="frame" v-if="showFrame" :transparentBg="true" :url="url.baseUrl" :path="url.path" :render="render" :data="paramData" @cancel="closeMeetEdit" @saveMeet="saveMeet" @cancelMeet="cancelMeet" @meetNotFound="meetNotFound" @isUploadedRecord="isUploadedRecord" @openRoom="openRoom" @closeRoom="closeRoom" height="100%"></tl-iframe>
    </div>
  </div>
</template>

<script>
/** mixins */
/** 子组件 */
/** 工具类 */
/** VUE配置 */
export default {
  name: 'meetEditComp',
  mixins: [],
  components: {},
  data() {
    return {
      display: false,
      url: {
        baseUrl: process.env.MEET_URL + '/dynamic',
        path: 'meet/meetManage/edit'
      },
      paramData: {},
      showFrame: true
    }
  },
  props: {
    sourceType: {
      type: String,
      default: ''
    },
    sourceId: {
      type: Number,
      default: 0
    },
    sourceApp: {
      type: String,
      default: ''
    },
    attendCodes: {
      type: String,
      default: ''
    },
    attendNames: {
      type: String,
      default: ''
    },
    disableEditSubmit: {
      type: Boolean,
      default: false
    },
    // 是否显示默认按钮
    isDisplayDefaultElement: {
      default: true,
      type: Boolean
    },
    // 会议id，新增时不用传
    meetId: {
      type: Number,
      default: 0
    },
    buttonName: {
      type: String,
      default: '发布会议'
    },
    render: {
      type: String,
      default: 'el-dialog'
    },
    width: {
      type: String,
      default: '1000px'
    },
    height: {
      type: String,
      default: '700px'
    }
  },
  computed: {
    isOpenMeet() {
      return !this.display && (this.meetId || this.attendCodes)
    }
  },
  watch: {
    isOpenMeet: {
      handler(newVal, oldVal) {
        this.resetParamData()
        this.$nextTick(() => {
          this.$refs.frame.open()
        })
      },
      immediate: true
    }
  },
  methods: {
    openMeetEdit() {
      this.display = true
    },
    closeMeetEdit() {
      this.display = false
      this.showFrame = false
      this.showFrame = true
      this.resetParamData()
      this.$nextTick(() => {
        this.$refs.frame.open()
      })
      this.$emit('refresh')
    },
    /** 暂存和发布成功后调用，返回model，需要在本系统中保存 **/
    saveMeet(model) {
      this.$emit('saveMeet', model)
      this.$emit('refresh')
    },
    /** 作废会议后调用，需要在本系统中删除 **/
    cancelMeet() {
      this.$emit('cancelMeet', this.meetId)
      this.$emit('refresh')
    },
    /** 若meetId对应的会议找不到了则调用该方法 **/
    meetNotFound() {
      this.cancelMeet()
      // 重置参数为新增会议
      this.paramData.id = 0
      this.paramData.isAdd = true
      this.paramData.visible = true
      this.$nextTick(() => {
        this.$refs.frame.open()
      })
    },
    /** 判断是否已上传会议纪要，并通知父组件 **/
    isUploadedRecord(res) {
      if (!this.meetId) {
        this.$emit('isUploadedRecord', res)
      }
    },
    openRoom() {
      this.$emit('update:height', parseInt(this.height.substring(0, this.height.length - 2)) + 200 + 'px')
    },
    closeRoom() {
      this.$emit('update:height', parseInt(this.height.substring(0, this.height.length - 2)) - 200 + 'px')
    },
    resetParamData() {
      this.paramData.id = this.meetId
      this.paramData.isAdd = !this.meetId
      this.paramData.visible = true
      this.paramData.microsourcetype = this.sourceType
      this.paramData.sourceid = this.sourceId
      this.paramData.sourceapp = this.sourceApp
      this.paramData.attendcodes = this.attendCodes
      this.paramData.attendnames = this.attendNames
      this.paramData.disableEditSubmit = this.disableEditSubmit
    }
  },
  mounted() {
    if (this.render === 'div') {
      this.openMeetEdit()
    }
  }
}
</script>
